<?php

class Join {

    const JOIN = "JOIN";
    const LEFT_JOIN = "LEFT JOIN";
    const RIGHT_JOIN = "RIGHT JOIN";

    /**
     * @var string
     */
    public $sourceTableName;

    /**
     * @var string
     */
    public $targetTableName;

    /**
     * @var CriteriaCollection
     */
    public $criterion;

    /**
     * @var string
     */
    public $type;

    /**
     * @param string $sourceTableName
     * @param string $targetTableName
     * @param Criteria $criteria
     * @param string $type
     */
    public function __construct($sourceTableName, $targetTableName, $criteria = null, $type = '') {
        $this->sourceTableName = $sourceTableName;
        $this->targetTableName = $targetTableName;

        $this->criterion = new CriteriaCollection();
        $criteria = !$criteria ? new Criteria($sourceTableName . '.id', Criteria::EQUAL, $targetTableName . '.id') : $criteria;
        $criteria->criteriaIsField = true;
        $this->criterion->addCriteria($criteria);

        $this->type = $type ? $type : self::JOIN;
    }

    /**
     * @return string
     */
    public function getJoinString() {
        return $this->type . ' ' . $this->targetTableName . ' ON ' .$this->criterion->getCriteriaString();
    }

}